package com.gz.steak.utils.http;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
/**
 * @Author 刘牌
 * @Date 2020/3/14 0014 13:33
 * @Version 1.0
 */

public class HttpUtil {
    /**
     * get请求
     * @return
     * @throws URISyntaxException
     * @throws IOException
     */
    public static JSONObject httpGet(String URL , ArrayList<BasicNameValuePair> list) throws URISyntaxException, IOException {
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        String param = EntityUtils.toString(new UrlEncodedFormEntity(list, Consts.UTF_8));
        HttpGet httpGet = new HttpGet(URL+param);
        CloseableHttpResponse response = closeableHttpClient.execute(httpGet);
        int code = response.getStatusLine().getStatusCode();
        HttpEntity entity = response.getEntity();
        String content = EntityUtils.toString(entity,"utf-8");
        JSONObject jsonObject = JSONObject.parseObject(content);
        return jsonObject;
    }
}
